package com.sy.olddriver.model;

import com.sy.olddriver.annotation.Controller;
import com.sy.olddriver.annotation.RequestMapping;
import com.sy.olddriver.annotation.ResponseBody;
import com.sy.olddriver.constant.AppConstant;
import com.sy.olddriver.entity.User;
import com.sy.olddriver.exception.BusinessException;
import com.sy.olddriver.service.CartService;
import com.sy.olddriver.service.HistoryService;
import com.sy.olddriver.service.UserService;
import com.sy.olddriver.service.impl.CartServiceImpl;
import com.sy.olddriver.service.impl.HistoryServiceImpl;
import com.sy.olddriver.service.impl.UserServiceImpl;
import com.sy.olddriver.utils.CookieUtils;
import com.sy.olddriver.utils.RedisUtils;
import com.sy.olddriver.utils.SMSUtils;
import com.sy.olddriver.utils.ValidateCodeUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
@RequestMapping("user")
public class UserModel {
    private UserService userService = new UserServiceImpl();
    private HistoryService historyService = new HistoryServiceImpl();
    private CartService cartService = new CartServiceImpl();

    @RequestMapping("regist")
    @ResponseBody
    public void registUser(HttpServletRequest req) {
        String correctValidateCode = String.valueOf(req.getSession().getAttribute(AppConstant.SESSION_MESSAGE_VALIDATE_CODE));
        userService.register(req.getParameter("name"), req.getParameter("pwd"), req.getParameter("pwdConfirm"),
                req.getParameter("phone"), req.getParameter("validateCode"), correctValidateCode);
    }

    @RequestMapping("checkNameDuplicate")
    @ResponseBody
    public boolean checkNameDuplicate(HttpServletRequest req) {
        return userService.checkNameDuplicate(req.getParameter("name"));
    }

    @RequestMapping("checkPhoneDuplicate")
    @ResponseBody
    public boolean checkPhoneDuplicate(HttpServletRequest req) {
        return userService.checkPhoneDuplicate(req.getParameter("phone"));
    }

    @RequestMapping("sendValidateCode")
    @ResponseBody
    public void sendValidateCode(HttpServletRequest req) {
        String validateCode = ValidateCodeUtils.getNum(4);
        req.getSession().setAttribute(AppConstant.SESSION_MESSAGE_VALIDATE_CODE, validateCode);
        SMSUtils.sendValidateCode(req.getParameter("phone"), validateCode);
    }

    @RequestMapping("login")
    @ResponseBody
    public User login(HttpServletRequest req, HttpServletResponse res) {
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        User user = userService.login(name, pwd);
        String token = UUID.randomUUID().toString().replace("-", "");
        CookieUtils.addCookie(AppConstant.COOKIE_USER_TOKEN_NAME, token, res);
        RedisUtils.setObj(AppConstant.REDIS_USER_KEY_PREFIX + token, user);
        //合并历史记录
        historyService.mergeHistory(req, res, user);
        //合并购物车记录
        cartService.mergeCart(req, res, user);
        return user;
    }

    @RequestMapping("loginUser")
    @ResponseBody
    public User getLoginUser(HttpServletRequest req) {
        String token = CookieUtils.getCookie(AppConstant.COOKIE_USER_TOKEN_NAME, req);
        if (StringUtils.isBlank(token)) {
            throw new BusinessException("用户尚未登录");
        } else {
            User user = RedisUtils.loadObj(AppConstant.REDIS_USER_KEY_PREFIX + token);
            if (user == null) {
                throw new BusinessException("用户尚未登录");
            }
            return user;
        }
    }

    @RequestMapping("logout")
    @ResponseBody
    public void logout(HttpServletRequest req, HttpServletResponse res) {
        String token = CookieUtils.getCookie(AppConstant.COOKIE_USER_TOKEN_NAME, req);
        CookieUtils.deleteCookie(AppConstant.COOKIE_USER_TOKEN_NAME, req, res);
        RedisUtils.del(AppConstant.REDIS_USER_KEY_PREFIX + token);
    }

}
